﻿using RuYiAdmin.Net.Repository.BusinessRepository.KafkaRepository;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace RuYiAdmin.Net.Service.BusinessService.KafkaService
{
    /// <summary>
    /// Kafka服务层实现
    /// </summary>
    public class KafkaService : IKafkaService
    {
        private readonly IKafkaRepository KafkaRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="KafkaRepository"></param>
        public KafkaService(IKafkaRepository KafkaRepository)
        {
            this.KafkaRepository = KafkaRepository;
        }

        /// <summary>
        /// 发布消息
        /// </summary>
        /// <typeparam name="TMessage">消息类型</typeparam>
        /// <param name="topicName">主题</param>
        /// <param name="message">消息</param>
        /// <returns></returns>
        public async Task PublishAsync<TMessage>(string topicName, TMessage message) where TMessage : class
        {
            await this.KafkaRepository.PublishAsync<TMessage>(topicName, message);
        }

        /// <summary>
        /// 订阅kafka
        /// </summary>
        /// <typeparam name="TMessage">消息类型</typeparam>
        /// <param name="topics">主题</param>
        /// <param name="messageFunc">回调函数</param>
        /// <param name="cancellationToken">取消口令</param>
        /// <returns></returns>
        public async Task SubscribeAsync<TMessage>(IEnumerable<string> topics, Action<TMessage> messageFunc, CancellationToken cancellationToken) where TMessage : class
        {
            await this.KafkaRepository.SubscribeAsync<TMessage>(topics, messageFunc, cancellationToken);
        }
    }
}
